import { defineStore } from "pinia";
import { getLinksListAPI } from "@/service/login/links";
import Swal from "sweetalert2";
import type { Link } from "@/service/login/types";
import cache from "@/utils/cache";
export const useLinksStore = defineStore("links", {
  state: () => ({
    linksList: [] as Link[],
    total: 0,
    totalPages: 0,
  }),
  actions: {
    async getLinksList(page: number, pageSize: number, token: string) {
      try {
        const token = cache.getCache("token");
        const res = await getLinksListAPI(page, pageSize, token);
        const { list, total, totalPages } = res.data;
        this.linksList = list;
        this.total = total;
        this.totalPages = totalPages;

        return res.data;
      } catch (error) {
        Swal.fire({
          title: "失敗",
          text: "链接列表获取失败",
          icon: "error",
        });
      }
    },
  },
});
